package com.usian.service.impl;

import com.usian.appointments.Appointments;
import com.usian.image.Images;
import com.usian.mapper.AppointmentsMapper;
import com.usian.mapper.ImagesMapper;
import com.usian.service.ImagesService;
import com.usian.user.User;
import com.usian.vo.ResultVo;
import org.jacoco.agent.rt.internal_43f5073.core.runtime.AgentOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ImagesServiceImpl implements ImagesService {
    private static final Logger log = LoggerFactory.getLogger(ImagesServiceImpl.class);
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private AppointmentsMapper appointmentsMapper;

    @Override
//    public List<Appointments> getImages() {
//
//        // 假设你的Images和Appointments类有合适的getter和setter方法等
//        List<Images> imagesList = imagesMapper.selectAll();
//        List<Appointments> appointmentsList = appointmentsMapper.findAll(null);
//
//        appointmentsList.forEach(appointments -> {
//            List<Images> matchedImagesList = imagesList.stream()
//                    .filter(images -> images.getAppointmentsId().equals(appointments.getId())
//                            && images.getUserId().equals(appointments.getUserId()))
//                    .collect(Collectors.toList());
//            appointments.setImagesList(matchedImagesList);
//        });
//
//        for (Appointments appointments1 : appointmentsList) {
//            if (appointments1.getUserId() != null) {
//                User user = appointmentsMapper.selectUserById(appointments1.getUserId());
//                if (user != null) {
//                    appointments1.setName(user.getName());
//                    appointments1.setImage(user.getImage());
//                    appointments1.setIntroduction(user.getIntroduction());
//                }
//            }
//        }
//
//
//        return appointmentsList;
//    }









    public List<Appointments> getImages() {

        // 获取所有 Images 和 Appointments 记录
        List<Images> imagesList = imagesMapper.selectAll();
        List<Appointments> appointmentsList = appointmentsMapper.findAll(null);

        appointmentsList.forEach(appointments -> {
            List<Images> matchedImagesList = imagesList.stream()
                    .filter(images -> images.getAppointmentsId() != null
                            && images.getAppointmentsId().equals(appointments.getId())
                            && images.getUserId().equals(appointments.getUserId()))
                    .collect(Collectors.toList());
            appointments.setImagesList(matchedImagesList);
        });

        for (Appointments appointments1 : appointmentsList) {
            if (appointments1.getUserId() != null) {
                User user = appointmentsMapper.selectUserById(appointments1.getUserId());
                if (user != null) {
                    appointments1.setName(user.getName());
                    appointments1.setImage(user.getImage());
                    appointments1.setIntroduction(user.getIntroduction());
                }
            }
        }

        return appointmentsList;
    }


    @Override
    public void addImages(Images images) {
        imagesMapper.addImages(images);
    }

}






